﻿using System;
using Ruici.BLL;
using RuiciWebSite.Models;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Configuration;
using WebGrease.Css.Extensions;

namespace RuiciWebSite.Controllers
{
    public class OrderExpertController : Controller
    {
        private readonly DocInfoBll _docInfoBll;
        public static string docImageUrl = ConfigurationManager.AppSettings["ImageUrl"];

        public OrderExpertController()
        {
            _docInfoBll = new DocInfoBll();
        }

        // GET: OrderExpert
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 步骤列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetDeptList()
        {
            List<DeptsViewModel> deptList = new List<DeptsViewModel>();

            var deptInfos = _docInfoBll.GetDeptList();
            if (deptInfos != null)
            {
                foreach (var item in deptInfos)
                {
                    DeptsViewModel deptInfo = new DeptsViewModel();
                    deptInfo.DeptID = item.id;
                    deptInfo.DeptName = item.name;
                    deptList.Add(deptInfo);
                }

            }
            return Json(deptList, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 标题列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetTitleList()
        {
            List<TitleListViewModel> titleList = new List<TitleListViewModel>();

            var titleInfos = _docInfoBll.GetTitleList();
            if (titleInfos != null)
            {
                foreach (var item in titleInfos)
                {
                    TitleListViewModel titleInfo = new TitleListViewModel();
                    titleInfo.TitleID = item.id;
                    titleInfo.TitleName = item.name;
                    titleList.Add(titleInfo);
                }

            }
            return Json(titleList, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 医生计划日期列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetActiveDateList()
        {
            var doctorOnTimeDateInfos = _docInfoBll.GetDoctorScheduleDates(-1, DateTime.Today, DateTime.Today.AddMonths(2));
            List<DateListViewModel> datelist = new List<DateListViewModel>();
            IFormatProvider format = new System.Globalization.CultureInfo("zh-CN");
            string TarStr = "yyyyMMdd";

            foreach (var dateitem in doctorOnTimeDateInfos)
            {
                DateListViewModel dateListViewModel = new DateListViewModel();
                DateTime dateinfo = DateTime.ParseExact(dateitem.ToString(), TarStr, format);
                dateListViewModel.ActiveDate = dateinfo;
                dateListViewModel.DateText = dateinfo.ToString("MM/dd") + ConvertDateWeek(dateinfo);
                datelist.Add(dateListViewModel);
            }
            return Json(datelist, JsonRequestBehavior.AllowGet);
        }

        public string ConvertDateWeek(DateTime datetime)
        {
            var dateinfoweekno = datetime.DayOfWeek;
            string returnValue = "";
            switch (dateinfoweekno)
            {
                case DayOfWeek.Sunday:
                    returnValue = " (周日)";
                    break;
                case DayOfWeek.Monday:
                    returnValue = " (周一)";
                    break;
                case DayOfWeek.Tuesday:
                    returnValue = " (周二)";
                    break;
                case DayOfWeek.Wednesday:
                    returnValue = " (周三)";
                    break;
                case DayOfWeek.Thursday:
                    returnValue = " (周四)";
                    break;
                case DayOfWeek.Friday:
                    returnValue = " (周五)";
                    break;
                case DayOfWeek.Saturday:
                    returnValue = " (周六)";
                    break;
            }

            return returnValue;
        }

        [HttpPost]
        public JsonResult GetActiveDoctorLists(OrderExpertPageInfoViewModel pageInfo)
        {
            //近期访问专家列表
            List<RecentVisitExpertsViewModel> doctorList = new List<RecentVisitExpertsViewModel>();

            if (string.IsNullOrWhiteSpace(pageInfo.date) && pageInfo.deptId == null && pageInfo.titleId == null &&
                string.IsNullOrWhiteSpace(pageInfo.keyWord))
            {
                //以上条件时，获取全部列表
                var doctorinfolist = _docInfoBll.GetDoctorList(-1, -1, "", 1, 10000);
                if (doctorinfolist.Count > 0)
                {
                    List<int> doctoridlist = new List<int>();

                    foreach (var item in doctorinfolist)
                    {
                        doctoridlist.Add(item.id);
                    }
                    var temp = doctoridlist.Select(x => "'" + x + "'");
                    string doctorid = string.Join(",", temp);
                    var doctorInfoList = _docInfoBll.GetDoctorListById(doctorid);
                    if (doctorInfoList != null)
                    {
                        foreach (var item in doctorInfoList)
                        {
                            RecentVisitExpertsViewModel doctorInfo = new RecentVisitExpertsViewModel();
                            doctorInfo.Id = item.id;
                            doctorInfo.AffiliatedHospital = item.affiliatedHospital;
                            doctorInfo.Name = item.name;
                            doctorInfo.TitleName = item.titleName;
                            doctorInfo.DeptName = item.deptName;
                            doctorInfo.DepName2 = item.depName2;
                            doctorInfo.SkillIn = string.IsNullOrWhiteSpace(item.skillIn) ? "" : item.skillIn;
                            doctorInfo.Image = docImageUrl + item.image;
                            doctorInfo.OrgName = item.orgName;
                            doctorInfo.DeptId = item.deptID;
                            doctorInfo.TitleID = item.titleID;
                            doctorList.Add(doctorInfo);
                        }
                    }
                    doctorList = doctorList.OrderBy(o => o.DepName2).ThenBy(o => o.Name)
                        .ToList();
                }
            }
            else
            {
                //日期不为空的情况
                if (!string.IsNullOrWhiteSpace(pageInfo.date))
                {
                    List<int> doctoridlist = new List<int>();

                    DateTime selectedDate = new DateTime();
                    if (!string.IsNullOrWhiteSpace(pageInfo.date))
                    {
                        DateTime dt = new DateTime(1970, 1, 1);
                        dt = dt.AddMilliseconds(long.Parse(pageInfo.date.Replace("/Date(", "").Replace(")/", "").Trim(' ')));
                        dt = dt.ToLocalTime();
                        selectedDate = dt;
                    }

                    var doctorOnTimeInfos = _docInfoBll.GetDoctorScheduleByDate(-1, selectedDate, selectedDate);

                    if (doctorOnTimeInfos.Count > 0)
                    {
                        foreach (var item in doctorOnTimeInfos)
                        {
                            if (!doctoridlist.Contains(item.docID))
                            {
                                doctoridlist.Add(item.docID);
                            }
                        }

                        if (pageInfo.deptId != null || pageInfo.titleId != null || !string.IsNullOrWhiteSpace(pageInfo.keyWord))
                        {
                            var doctorinfolist = _docInfoBll.GetDoctorList(pageInfo.deptId, pageInfo.titleId, pageInfo.keyWord, 1, 1000);
                            if (doctorinfolist.Count > 0)
                            {
                                List<int> doctoridlisttemp = new List<int>();
                                foreach (var item in doctorinfolist)
                                {
                                    doctoridlisttemp.Add(item.id);
                                }

                                var doctoridlists = doctoridlisttemp.Intersect(doctoridlist).ToList();
                                doctoridlist.Clear();
                                foreach (int item in doctoridlists)
                                {
                                    doctoridlist.Add(item);
                                }
                            }
                        }

                        var temp = doctoridlist.Select(x => "'" + x + "'");
                        string doctorids = string.Join(",", temp);
                        var doctorInfoLists = _docInfoBll.GetDoctorListById(doctorids);
                        if (doctorInfoLists != null)
                        {
                            foreach (var item in doctorInfoLists)
                            {
                                RecentVisitExpertsViewModel doctorInfo = new RecentVisitExpertsViewModel();
                                doctorInfo.Id = item.id;
                                doctorInfo.AffiliatedHospital = item.affiliatedHospital;
                                doctorInfo.Name = item.name;
                                doctorInfo.TitleName = item.titleName;
                                doctorInfo.DeptName = item.deptName;
                                doctorInfo.DepName2 = item.depName2;
                                doctorInfo.SkillIn = string.IsNullOrWhiteSpace(item.skillIn)
                                    ? ""
                                    : (item.skillIn.Count() > 55
                                        ? item.skillIn.Substring(0, 55) + "..."
                                        : item.skillIn);
                                doctorInfo.Image = docImageUrl + item.image;
                                doctorInfo.OrgName = item.orgName;
                                doctorInfo.DeptId = item.deptID;
                                doctorInfo.TitleID = item.titleID;
                                doctorList.Add(doctorInfo);
                            }
                        }
                    }
                    
                    if (pageInfo.deptId != null || pageInfo.titleId != null || !string.IsNullOrWhiteSpace(pageInfo.keyWord))
                    {
                        if (pageInfo.deptId != null)
                        {
                            doctorList = doctorList.Where((m => m.DeptName == pageInfo.deptName)).ToList();
                        }
                        if (pageInfo.titleId != null)
                        {
                            doctorList = doctorList.Where((m => m.TitleName == pageInfo.titleName)).ToList();
                        }
                        if (!string.IsNullOrWhiteSpace(pageInfo.keyWord))
                        {
                            doctorList = doctorList.Where((m => m.Name == pageInfo.keyWord || m.Id == pageInfo.keyWord)).ToList();
                        }
                    }
                }
                else
                {
                    //日期为空，（步骤，标题或关键词不为空的情况）
                    if (pageInfo.deptId != null || pageInfo.titleId != null || !string.IsNullOrWhiteSpace(pageInfo.keyWord))
                    {
                        var doctorinfolist = _docInfoBll.GetDoctorList(pageInfo.deptId, pageInfo.titleId, pageInfo.keyWord, pageInfo.pageCount, 1000);
                        if (doctorinfolist.Count > 0)
                        {
                            List<int> doctoridlist = new List<int>();

                            foreach (var item in doctorinfolist)
                            {
                                doctoridlist.Add(item.id);
                            }
                            var temp = doctoridlist.Select(x => "'" + x + "'");
                            string doctorid = string.Join(",", temp);
                            var doctorInfoList = _docInfoBll.GetDoctorListById(doctorid);
                            if (doctorInfoList != null)
                            {
                                foreach (var item in doctorInfoList)
                                {
                                    RecentVisitExpertsViewModel doctorInfo = new RecentVisitExpertsViewModel();
                                    doctorInfo.Id = item.id;
                                    doctorInfo.AffiliatedHospital = item.affiliatedHospital;
                                    doctorInfo.Name = item.name;
                                    doctorInfo.TitleName = item.titleName;
                                    doctorInfo.DeptName = item.deptName;
                                    doctorInfo.DepName2 = item.depName2;
                                    doctorInfo.SkillIn = string.IsNullOrWhiteSpace(item.skillIn) ? "" : item.skillIn;
                                    doctorInfo.Image = docImageUrl + item.image;
                                    doctorInfo.OrgName = item.orgName;
                                    doctorInfo.DeptId = item.deptID;
                                    doctorList.Add(doctorInfo);
                                }
                            }
                        }
                    }
                }
            }

            int iTotalCount = doctorList.Count;

            pageInfo.totalCount = iTotalCount;
            var pagecount = (iTotalCount + pageInfo.pageSize - 1) / pageInfo.pageSize;
            pageInfo.pageCount = pagecount;
            if (iTotalCount > 0)
            {
                var list = doctorList.OrderBy(o => o.DepName2).Skip(pageInfo.pageCurrent * pageInfo.pageSize - pageInfo.pageSize).Take(pageInfo.pageSize).ToList();
                pageInfo.curPageRecord = list;
            }
            else
            {
                //分页
                var list = doctorList.Skip(pageInfo.pageCurrent * pageInfo.pageSize - pageInfo.pageSize).Take(pageInfo.pageSize).ToList();
                pageInfo.curPageRecord = list;
            }
            if (pageInfo != null)
                return Json(pageInfo, JsonRequestBehavior.AllowGet);

            return Json(new ReturnCode(-1, "没有相关条件医生"), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult GetAllActiveDoctorLists(OrderExpertPageInfoViewModel pageInfo)
        {
            //近期访问专家列表
            List<RecentVisitExpertsViewModel> doctorList = new List<RecentVisitExpertsViewModel>();

            var doctorinfolist = _docInfoBll.GetDoctorList(-1, -1, "", 1, 10000);
            if (doctorinfolist.Count > 0)
            {
                List<int> doctoridlist = new List<int>();

                foreach (var item in doctorinfolist)
                {
                    doctoridlist.Add(item.id);
                }
                var temp = doctoridlist.Select(x => "'" + x + "'");
                string doctorid = string.Join(",", temp);
                var doctorInfoList = _docInfoBll.GetDoctorListById(doctorid);
                if (doctorInfoList != null)
                {
                    foreach (var item in doctorInfoList)
                    {
                        RecentVisitExpertsViewModel doctorInfo = new RecentVisitExpertsViewModel();
                        doctorInfo.Id = item.id;
                        doctorInfo.AffiliatedHospital = item.affiliatedHospital;
                        doctorInfo.Name = item.name;
                        doctorInfo.TitleName = item.titleName;
                        doctorInfo.DeptName = item.deptName;
                        doctorInfo.DepName2 = item.depName2;
                        doctorInfo.SkillIn = string.IsNullOrWhiteSpace(item.skillIn) ? "" : item.skillIn;
                        doctorInfo.Image = docImageUrl + item.image;
                        doctorInfo.OrgName = item.orgName;
                        doctorInfo.DeptId = item.deptID;
                        doctorInfo.TitleID = item.titleID;
                        doctorList.Add(doctorInfo);
                    }
                }
                doctorList = doctorList.OrderBy(o => o.DepName2).ThenBy(o => o.Name).ToList();
            }


            int iTotalCount = doctorList.Count;

            pageInfo.totalCount = iTotalCount;
            var pagecount = (iTotalCount + pageInfo.pageSize - 1) / pageInfo.pageSize;
            pageInfo.pageCount = pagecount;
            if (iTotalCount > 0)
            {
                var list = doctorList.OrderBy(o => o.DepName2).Skip(pageInfo.pageCurrent * pageInfo.pageSize - pageInfo.pageSize).Take(pageInfo.pageSize).ToList();
                pageInfo.curPageRecord = list;
            }
            else
            {
                var list = doctorList.Skip(pageInfo.pageCurrent * pageInfo.pageSize - pageInfo.pageSize).Take(pageInfo.pageSize).ToList();
                pageInfo.curPageRecord = list;
            }
            if (pageInfo != null)
                return Json(pageInfo, JsonRequestBehavior.AllowGet);

            return Json(new ReturnCode(-1, "没有相关条件医生"), JsonRequestBehavior.AllowGet);
        }




        [HttpPost]
        public JsonResult GetActiveDoctorList(string date, int? deptId, string deptName, int? titleId, string titleName, string keyWord, int page = 1, int pageSize = 10)
        {
            List<RecentVisitExpertsViewModel> doctorList = new List<RecentVisitExpertsViewModel>();

            if (!string.IsNullOrWhiteSpace(date))
            {
                List<int> doctoridlist = new List<int>();

                DateTime selectedDate = new DateTime();
                if (!string.IsNullOrWhiteSpace(date))
                {
                    DateTime dt = new DateTime(1970, 1, 1);
                    dt = dt.AddMilliseconds(long.Parse(date.Replace("/Date(", "").Replace(")/", "").Trim(' ')));
                    dt = dt.ToLocalTime();
                    selectedDate = dt;
                }

                if (deptId != null || titleId != null || !string.IsNullOrWhiteSpace(keyWord))
                {
                    pageSize = 100;
                    var doctorinfolist = _docInfoBll.GetDoctorList(deptId, titleId, keyWord, page, pageSize);
                    if (doctorinfolist.Count > 0)
                    {
                        foreach (var item in doctorinfolist)
                        {
                            doctoridlist.Add(item.id);
                        }
                    }
                }
                //值班医生
                var doctorOnTimeInfos = _docInfoBll.GetDoctorScheduleByDate(-1, selectedDate, selectedDate);
                if (doctorOnTimeInfos.Count > 0)
                {
                    foreach (var item in doctorOnTimeInfos)
                    {
                        if (!doctoridlist.Contains(item.docID))
                        {
                            doctoridlist.Add(item.docID);
                        }
                    }
                    var temp = doctoridlist.Select(x => "'" + x + "'");
                    string doctorids = string.Join(",", temp);
                    var doctorInfoLists = _docInfoBll.GetDoctorListById(doctorids);
                    if (doctorInfoLists != null)
                    {
                        foreach (var item in doctorInfoLists)
                        {
                            RecentVisitExpertsViewModel doctorInfo = new RecentVisitExpertsViewModel();
                            doctorInfo.Id = item.id;
                            doctorInfo.AffiliatedHospital = item.affiliatedHospital;
                            doctorInfo.Name = item.name;
                            doctorInfo.TitleName = item.titleName;
                            doctorInfo.DeptName = item.deptName;
                            doctorInfo.DepName2 = item.depName2;
                            doctorInfo.SkillIn = string.IsNullOrWhiteSpace(item.skillIn) ? "" : item.skillIn;
                            doctorInfo.Image = docImageUrl + item.image;
                            doctorInfo.OrgName = item.orgName;
                            doctorInfo.DeptId = item.deptID;
                            doctorInfo.TitleID = item.titleID;
                            doctorList.Add(doctorInfo);
                        }
                    }
                }

                if (deptId != null || titleId != null || !string.IsNullOrWhiteSpace(keyWord))
                {
                    if (deptId != null)
                    {
                        doctorList = doctorList.Where((m => m.DeptName == deptName.ToString())).ToList();
                    }
                    if (titleId != null)
                    {
                        doctorList = doctorList.Where((m => m.TitleName == titleName.ToString())).ToList();
                    }
                    if (!string.IsNullOrWhiteSpace(keyWord))
                    {
                        doctorList = doctorList.Where((m => m.Name == keyWord || m.Id == keyWord)).ToList();
                    }
                }
            }
            else
            {
                if (deptId != null || titleId != null || !string.IsNullOrWhiteSpace(keyWord))
                {
                    var doctorinfolist = _docInfoBll.GetDoctorList(deptId, titleId, keyWord, page, pageSize);
                    if (doctorinfolist.Count > 0)
                    {
                        List<int> doctoridlists = new List<int>();

                        foreach (var item in doctorinfolist)
                        {
                            doctoridlists.Add(item.id);
                        }
                        var temp = doctoridlists.Select(x => "'" + x + "'");
                        string doctorid = string.Join(",", temp);
                        var doctorInfoList = _docInfoBll.GetDoctorListById(doctorid);
                        if (doctorInfoList != null)
                        {
                            foreach (var item in doctorInfoList)
                            {
                                RecentVisitExpertsViewModel doctorInfo = new RecentVisitExpertsViewModel();
                                doctorInfo.Id = item.id;
                                doctorInfo.AffiliatedHospital = item.affiliatedHospital;
                                doctorInfo.Name = item.name;
                                doctorInfo.TitleName = item.titleName;
                                doctorInfo.DeptName = item.deptName;
                                doctorInfo.DepName2 = item.depName2;
                                doctorInfo.SkillIn = string.IsNullOrWhiteSpace(item.skillIn) ? "" : item.skillIn;
                                doctorInfo.Image = docImageUrl + item.image;
                                doctorInfo.OrgName = item.orgName;
                                doctorInfo.DeptId = item.deptID;
                                doctorInfo.TitleID = item.titleID;
                                doctorList.Add(doctorInfo);
                            }
                        }
                    }
                }
            }

            return Json(doctorList, JsonRequestBehavior.AllowGet);
        }
    }
}